package com.swz.dcrm.adpter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.Insurance;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.PARENT_ID;

public class InsuranceAdapter extends CustomAdapter<Insurance> {
    private ActionSheetDialog dialog;

    private List<Insurance>selected;

    private Map<Integer,Insurance> map=new HashMap<>();
    public InsuranceAdapter(Context context, List<Insurance> datas) {
        super(context, R.layout.item_insurance, datas);
    }

    @Override
    protected void convert(ViewHolder holder, Insurance insurance, int position) {
        CheckBox cb=holder.getView(R.id.cb);


        cb.setOnCheckedChangeListener((buttonView, isChecked) -> {

            if(isChecked){
               if(insurance.getChildren()==null||insurance.getChildren().size()==0){
                   map.put(position,insurance);
               }else {
                   if(!map.containsKey(position)){
                       map.put(position,insurance.getChildren().get(0));
                   }
               }
            }else {
                map.remove(position);
            }
            mOnItemClickListener.onItemClick(buttonView,holder,position);
        });
        if(insurance.getChildren()!=null&&insurance.getChildren().size()>0){
            if(selected!=null){
                for(Insurance child:selected){
                    if(insurance.getId()==child.getPid()){
                        map.put(position,child);
                        cb.setChecked(true);
                        holder.setText(R.id.tv_child,child.getInsuranceDesc());
                        holder.setText(R.id.tv_insurance_fee,child.getPrice());
                    }
                }
            }else {
                cb.setChecked(true);
                map.put(position,insurance.getChildren().get(0));
                holder.setText(R.id.tv_child,insurance.getChildren().get(0).getInsuranceDesc());
                holder.setText(R.id.tv_insurance_fee,insurance.getChildren().get(0).getPrice());
            }
            holder.getView(R.id.tv_child).setVisibility(View.VISIBLE);
            mOnItemClickListener.onItemClick(holder.itemView,holder,position);
        }else {
           if(selected!=null){
               for(Insurance child:selected){
                   if(insurance.getId()==child.getId()){
                      cb.setChecked(true);
                   }
               }
           }
            holder.setText(R.id.tv_insurance_fee,insurance.getPrice());
            holder.getView(R.id.tv_child).setVisibility(View.GONE);
            ConstraintLayout.LayoutParams lp= (ConstraintLayout.LayoutParams) holder.getView(R.id.tv_insurance_fee).getLayoutParams();
            lp.bottomToBottom=PARENT_ID;
            holder.getView(R.id.tv_insurance_fee).setLayoutParams(lp);
        }

        holder.setText(R.id.tv_insurance_name,insurance.getInsuranceDesc());

        holder.itemView.setOnClickListener(v -> {
            if(cb.isChecked()&&insurance.getChildren()!=null&&insurance.getChildren().size()>0){
                List<String> strings=new ArrayList<>();
                for(Insurance i:insurance.getChildren()){
                    strings.add(i.getInsuranceDesc());
                }
                dialog=new ActionSheetDialog(mContext,strings.toArray(new String[strings.size()]),null);
                dialog.setOnOperItemClickL((parent, view, position1, id) -> {
                    map.put(position,insurance.getChildren().get(position1));
                    mOnItemClickListener.onItemClick(view,holder,position);
                    holder.setText(R.id.tv_insurance_fee,insurance.getChildren().get(position1).getPrice());
                    holder.setText(R.id.tv_child,insurance.getChildren().get(position1).getInsuranceDesc());
                    dialog.dismiss();
                });

                dialog.isTitleShow(false);
                dialog.show();
            }else {
                cb.setChecked(!cb.isChecked());
            }
        });
    }
    public void setSelected(List<Insurance> selected){
        this.selected=selected;
    }

    public List<Insurance> getSelected(){
        return new ArrayList<>(map.values());
    }
    public String getSelectedTotalFee(){
        if(map.values()==null||map.values().size()==0){
            return "¥0";
        }
        List<Insurance> insurances=new ArrayList<>(map.values());
        BigDecimal bigDecimal=null;
       for(Insurance insurance:insurances){
          if(bigDecimal==null){
              bigDecimal=new BigDecimal(insurance.getPrice());
          }else {
              bigDecimal=bigDecimal.add(new BigDecimal(insurance.getPrice()));
          }
       }
       return "¥"+bigDecimal.toString();
    }
}
